package ru.poomsae.isap.Model

import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.androidnetworking.AndroidNetworking
import com.androidnetworking.common.Priority
import com.androidnetworking.error.ANError
import com.androidnetworking.interfaces.JSONObjectRequestListener
import com.example.isap_wtf.Model.ApiEndPoint
import com.example.isap_wtf.Model.ParametrsISAP
import org.json.JSONObject
import java.util.*

class paraJSON (mTextView: TextView, mLinearLayout: LinearLayout, mRelativeLayout: LinearLayout, mySudTextView: TextView) {

    var idHost: String = ParametrsISAP.idHostServer
    var sys_sud = ParametrsISAP.sys_sud
    var idvistuplenie: String = ""
    var myTimer: Timer = Timer()
    var myTimerSud: Timer = Timer()
    var textView: TextView = mTextView
    var linearLayout: LinearLayout = mLinearLayout
    var relativeLayout: LinearLayout = mRelativeLayout
    var sudTextView: TextView = mySudTextView
    var numSud = ParametrsISAP.numSud
    var kol_sud = ParametrsISAP.kol_sud
    var kolSudConnect = ""
    var pokaz = 0
    var nameSport = ""
    var poomsae = ""
    var success = 0

    fun waitVistup() {
        AndroidNetworking.get("http://" + idHost + ApiEndPoint.VISTUPLENIEA)
            .setPriority(Priority.MEDIUM)
            .build()
            .getAsJSONObject(object : JSONObjectRequestListener {
                override fun onResponse(response: JSONObject?) {
                    try {
                        idvistuplenie = response?.getString("success")!!
                        pokaz = response?.getInt("vistuplenie_pokaz")
//                    nameSport = response?.getString("vistuplenie_sportsmen")
                        poomsae = response?.getString("phumse")
                        ParametrsISAP.pokaz = pokaz
                        ParametrsISAP.sportName = nameSport
                        if (idvistuplenie == "null") {
                            myTimer.schedule(object : TimerTask() {
                                override fun run() {
                                    waitVistupID()
                                }
                            }, 1, 1000)
                        } else {
                            waitSudGotov()
                        }
                    } catch (e: Exception) {
                        println("pokaz = " + "null")
                        waitVistup()
                    }

                }

                override fun onError(anError: ANError?) {
//                    println("anError = " + anError)
                }
            })
    }

    fun waitVistupID() {
        AndroidNetworking.get("http://" + idHost + ApiEndPoint.VISTUPLENIEA)
            .setPriority(Priority.MEDIUM)
            .build()
            .getAsJSONObject(object : JSONObjectRequestListener {
                override fun onResponse(response: JSONObject?) {
                    try {
                        idvistuplenie = response?.getString("success")!!
                        pokaz = response?.getInt("vistuplenie_pokaz")
//                    nameSport = response?.getString("vistuplenie_sportsmen")
//                        poomsae = response?.getString("phumse")
                        ParametrsISAP.pokaz = pokaz
                        ParametrsISAP.sportName = nameSport
                        if (idvistuplenie != "null") {
                            waitSud()
                        }
                    } catch (e: Exception) {
                        println("pokaz = " + "null")
                        waitVistupID()
                    }

                }

                override fun onError(anError: ANError?) {
//                    println("anError = " + anError)
                }
            })
    }

    fun waitSudGotov() {
        myTimer.cancel()
        textView.setText("Judges Connection...")
        AndroidNetworking.post("http://" + idHost + ApiEndPoint.SUDIA)
            .addBodyParameter("vistuplenie_idvistuplenie", idvistuplenie)
            .addBodyParameter("nomersud", numSud)
            .setPriority(Priority.MEDIUM)
            .build()
            .getAsJSONObject(object : JSONObjectRequestListener {
                override fun onResponse(response: JSONObject?) {
                    waitSud()
                    ParametrsISAP.idvistuplenie = idvistuplenie
                }

                override fun onError(anError: ANError?) {
                    println("anError = " + anError)
                }
            })
    }

    fun waitSud() {
        AndroidNetworking.post("http://" + idHost + ApiEndPoint.SUDIGOTOVA)
            .addBodyParameter("vistuplenie_idvistuplenie", idvistuplenie)
            .setPriority(Priority.MEDIUM)
            .build()
            .getAsJSONObject(object : JSONObjectRequestListener {
                override fun onResponse(response: JSONObject?) {
                    kolSudConnect = response?.getString("success")!!
                    if (kolSudConnect != kol_sud) {
                        myTimerSud.schedule(object : TimerTask() {
                            override fun run() {
                                println("waitsud Timer")
                                waitSudConn()
                            }
                        }, 1, 1000)
                    } else {
                        visibleSys()
                    }
                }
                override fun onError(anError: ANError?) {
//                    Log.d("ON ERROR", anError?.errorDetail?.toString())
                }
            })
    }

    fun waitSudConn() {
        AndroidNetworking.post("http://" + idHost + ApiEndPoint.SUDIGOTOVA)
            .addBodyParameter("vistuplenie_idvistuplenie", idvistuplenie)
            .setPriority(Priority.MEDIUM)
            .build()
            .getAsJSONObject(object : JSONObjectRequestListener {
                override fun onResponse(response: JSONObject?) {
                    kolSudConnect = response?.getString("success")!!
                    if (kolSudConnect == kol_sud) {
                        visibleSys()
                    }
                }
                override fun onError(anError: ANError?) {
//                    Log.d("ON ERROR", anError?.errorDetail?.toString())
                }
            })
    }

    fun visibleSys() {
        myTimerSud.cancel()
        linearLayout.visibility = View.GONE
        relativeLayout.visibility = View.VISIBLE
        sudTextView.setText("Judges № " + numSud)
    }

//    fun delSudGotov() {
//        AndroidNetworking.post("http://" + idHost + ApiEndPoint.SETTINGRES)
//            .addBodyParameter("vistuplenie_idvistuplenie", idvistuplenie)
//            .addBodyParameter("kol_sud", kol_sud)
//            .setPriority(Priority.MEDIUM)
//            .build()
//            .getAsJSONObject(object : JSONObjectRequestListener {
//                override fun onResponse(response: JSONObject?) {
//                    try {
//                        success = response?.getInt("success")!!
//                        println("success = " + success)
//                        waitSudGotov()
//                    } catch (e: Exception) {
//                        println("pokaz = " + "null")
//                    }
//                }
//
//                override fun onError(anError: ANError?) {
//                    println("anError = " + anError)
//                }
//            })
//    }
}